package com.example.houduan.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.houduan.entity.User;
import com.example.houduan.mapper.UserMapper;
import org.springframework.stereotype.Service;

@Service
public class UserService extends ServiceImpl<UserMapper, User> {
    public User getUserByusername(User user1){
        // 根据用户名查询用户
        // 避免客户端传入 id，强制设置为 null 使用数据库自增
        user1.setId(null);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, user1.getUsername());
        User user2 = this.getOne(queryWrapper);
        if(user2!=null){
            if (user2.getPassword().equals(user1.getPassword())){
                return user2;
            }
            else{
                return null;
            }
        }
        else {
            return null;
        }
    }

    // 注册
    public Integer registry(User user){
        // 根据用户名查询用户
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, user.getUsername());
        User user1 = this.getOne(queryWrapper);
        if(user1==null){
            if(this.save(user)){
                return 1;  //注册成功
            }
            else{
                return -1;  //注册失败
            }
        }
        else {
            //用户已经存在
            return 0;
        }
    }

}
